const path = require('path')
const os = require('os');
const fs = require('fs');
const common = require('./webpack.common.js');
const { merge } = require('webpack-merge');
const imgtocss = require('./util/imgtocss.js');
const TransferWebpackPlugin = require("transfer-webpack-plugin");

// 重新npm run dev 清空上次生成的文件内容
fs.writeFileSync('./src/template/components/plugin/all_link.ejs', '');
fs.writeFileSync('./src/template/components/plugin/all_script.ejs', '');
fs.writeFileSync('./src/template/components/data/hdata.js', '');
fs.writeFileSync('./src/js/main/index.js', '');

function getIpAddr() {
  var interfaces = os.networkInterfaces();
  for (var devName in interfaces) {
    var iface = interfaces[devName];
    for (var i = 0; i < iface.length; i++) {
      var alias = iface[i];
      if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
        return alias.address;
      }
    }
  }
};

module.exports = merge(imgtocss, common, {
  mode: 'development',
  devtool: 'inline-source-map', //none、eval、cheap-module-eval-source-map
  devServer: {
    contentBase: path.resolve(__dirname, 'dist'),
    // host: getIpAddr(),
    host: 'localhost',
    port: '8088',  // 设置端口号，注意，如果端口号被占用会报错
    // https: true,
    inline: true,
    historyApiFallback: true,
    // hot: true,
  },
  plugins: [
    new TransferWebpackPlugin([
      {
        from: `js/static`,
        to: `js`
      }
    ], path.resolve(__dirname, "src"))
  ],

});

